<?php

namespace App\Console;

use App\Models\XS\JobXS;
use App\Models\XS\ResumeXS;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
//         Commands\Inspire::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        //如果默认是并行则不需要runInBackground,如果默认是串行则需要runInBackground
        /**
         * 职位表更新到迅搜
         */
        $schedule->call(function () {
            JobXS::timerUpdate();
        })
            ->everyMinute()//每分钟执行
            ->when(function () {
                //如果给定闭包返回true，
                //只要没有其它约束条件阻止任务运行，该任务就会执行
                //false终止
                return boolval(\Site::get('xsOpen')) && boolval(\Site::get('xunSearchJobTimerStatus'));
            })
            ->name('job')//任务唯一标志
            ->withoutOverlapping()//上一次任务如果没有结束,则不执行
            ->runInBackground();
        //https://laravel-china.org/articles/4055/laravel-timing-task
        //默认情况下，即使前一个任务仍然在运行调度任务也会运行，
        //要避免这样的情况，可使用withoutOverlapping方法

        /**
         * 简历表更新到迅搜
         */
        $schedule->call(function () {
            ResumeXS::timerUpdate();
        })
            ->everyMinute()
            ->when(function () {
                return boolval(\Site::get('xsOpen')) && boolval(\Site::get('xunSearchResumeTimerStatus'));
            })
            ->name('resume')
            ->withoutOverlapping()
            ->runInBackground();
    }
}
